package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.pay.PayTypeConfigVO;
import com.qumitech.visualnovel.pubdef.dto.pay.*;
import com.qumitech.visualnovel.service.pay.PayConfigService;
import com.qumitech.visualnovel.service.pay.PayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/3/11 11:15
 */

@RestController
@RequestMapping(value = "/api/pay")
public class PayController {

    @Autowired
    private PayConfigService payConfigService;
    @Autowired
    private PayService payService;


    @GetMapping("/type")
    public BaseRsp getPayConfig() {
        return new BaseRsp(ResultEnum.SUCCESS, payConfigService.listPayTypeConfigVO());
    }

    @PostMapping("/type")
    public BaseRsp addPayConfig(@RequestBody PayTypeConfigVO payTypeConfigVO) {
        payConfigService.addPayTypeConfig(payTypeConfigVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("type/{paytype}")
    public BaseRsp removePayConfig(@PathVariable(name = "paytype") String payType) {
        payConfigService.removePayTypeConfig(payType);
        return new BaseRsp(ResultEnum.SUCCESS);
    }
    @PutMapping(value = "type/{paytype}")
    public BaseRsp updateButtonConfig(@RequestBody PayTypeConfigVO payTypeConfigVO, @PathVariable(name = "paytype") String payType) {
        payTypeConfigVO.setPayType(payType);
        payConfigService.updatePayTypeConfig(payTypeConfigVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }
    @RequiresPermissions("payAccount:view")
    @GetMapping("/account/list")
    public BaseRsp listUserPayAccountAdmin(@RequestParam(value = "limit", required = false, defaultValue = "20") String limit,
                                           @RequestParam(value = "offset", required = false, defaultValue = "0") String offset,
                                           @RequestParam(value = "uid", required = false) String uid,
                                           @RequestParam(value = "pay_type", required = false) String payType,
                                           @RequestParam(value = "startTime", required = false) String startTime,
                                           @RequestParam(value = "endTime", required = false) String endTime){
        Map<String, String> paramMap = getGeneralParamMap(limit, offset, uid, startTime, endTime);
        if(payType != null){
            paramMap.put("pay_type", payType);
        }
        return payService.listUserPayAccountAdmin(paramMap);
    }
    @RequiresPermissions("payAccountBlack:view")
    @GetMapping("/account/black/list")
    public BaseRsp listPayBlackUserAdmin(@RequestParam(value = "limit", required = false, defaultValue = "20") String limit,
                                         @RequestParam(value = "offset", required = false, defaultValue = "0") String offset,
                                         @RequestParam(value = "uid", required = false) String uid,
                                         @RequestParam(value = "state", required = false) String state,
                                         @RequestParam(value = "startTime", required = false) String startTime,
                                         @RequestParam(value = "endTime", required = false) String endTime){
        Map<String, String> paramMap = getGeneralParamMap(limit, offset, uid, startTime, endTime);
        if(state != null){
            paramMap.put("state", state);
        }
        return payService.listPayBlackUserAdmin(paramMap);
    }

    @PostMapping("/account/black")
    public BaseRsp addPayBlackUserAdmin(@RequestBody PayBlackUserAdminAddDTO dto){
        return payService.addPayBlackUserAdmin(dto);
    }

    @PutMapping("/account/black")
    public BaseRsp updatePayBlackUserAdmin(@RequestBody PayBlackUserAdminUpdateDTO dto){
        return payService.updatePayBlackUserAdmin(dto);
    }


    @GetMapping("/type/list")
    public BaseRsp listPayType() {
        return payService.listPayType();
    }


    @GetMapping("/plan/list")
    public BaseRsp listPayPlan(PayPlanAdminQueryDTO queryDTO) {
        return payService.listPayPlan(queryDTO);
    }


    @PostMapping("/plan")
    public BaseRsp savePayPlan(@RequestBody PayPlanAdminDTO adminDTO) {
        return payService.savePayPlan(adminDTO);
    }


    @PutMapping("/plan/{plan_id}")
    public BaseRsp updatePayPlan(@RequestBody PayPlanAdminDTO adminDTO,
                                 @PathVariable(name = "plan_id") String planId) {
        adminDTO.setPlanId(planId);
        return payService.updatePayPlan(adminDTO);
    }


    @DeleteMapping("/plan/{plan_id}")
    public BaseRsp rmPayPlan(@PathVariable(name = "plan_id") String planId) {
        PayPlanAdminDTO adminDTO = new PayPlanAdminDTO();
        adminDTO.setPlanId(planId);
        return payService.rmPayPlan(adminDTO);
    }

    @GetMapping("/application/list")
    public BaseRsp listPayApplication(PayApplicationAdminQueryDTO queryDTO) {
        return payService.listPayApplication(queryDTO);
    }


    @PostMapping("/application")
    public BaseRsp savePayApplication(@RequestBody PayApplicationAdminDTO adminDTO) {
        return payService.savePayApplication(adminDTO);
    }


    @PutMapping("/application/{application_id}")
    public BaseRsp updatePayApplication(@RequestBody PayApplicationAdminDTO adminDTO,
                                        @PathVariable(name = "application_id") String applicationId) {
        adminDTO.setApplicationId(applicationId);
        return payService.updatePayApplication(adminDTO);
    }


    @DeleteMapping("/application/{application_id}")
    public BaseRsp rmPayApplication(@PathVariable(name = "application_id") String applicationId) {
        PayApplicationAdminDTO adminDTO = new PayApplicationAdminDTO();
        adminDTO.setApplicationId(applicationId);
        return payService.rmPayApplication(adminDTO);
    }



    private Map<String, String> getGeneralParamMap(String limit, String offset, String uid, String startTime, String endTime){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("limit", limit);
        paramMap.put("offset", offset);
        if(uid != null){
            paramMap.put("uid", uid);
        }
        if(startTime != null){
            paramMap.put("start_time", startTime);
        }
        if(endTime != null){
            paramMap.put("end_time", endTime);
        }
        return paramMap;
    }

}
